package com.example.beat_x3.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.R;

import java.util.ArrayList;

import Entity.Inf;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.Holder> {

    public ArrayList<String> message;
    public ArrayList<Boolean> sender;
    int end;

    public ChatAdapter(ArrayList<String> message, ArrayList<Boolean> sender) {
        this.message = message;
        this.sender = sender;
        end = message.size();
    }

    @NonNull
    @Override
    public ChatAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ChatAdapter.Holder(LayoutInflater.from(Inf.context).inflate(viewType, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ChatAdapter.Holder holder, int position) {
        holder.bind(position);
    }

    public void refresh() {

        notifyItemRangeChanged(end,message.size() - end);
        end = message.size();
    }

    @Override
    public int getItemViewType(int position) {
        return sender.get(position) ? R.layout.layout_chat1 : R.layout.layout_chat2;
    }

    @Override
    public int getItemCount() {
        return message.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView chatText;

        public Holder(@NonNull View itemView) {
            super(itemView);
            chatText = itemView.findViewById(R.id.chat_word);
        }

        void bind(int position) {
            chatText.setText(message.get(position));
        }
    }
}
